package com.in.mobilerechargedth;



import java.util.List;


import com.in.mobilerechargedth.adapter.SpinnerOperatorAdapter;
import com.in.mobilerechargedth.api.OperatorApi;
import com.in.mobilerechargedth.common.CustomSharedPreferences;
import com.in.mobilerechargedth.constant.ConstantVariables;
import com.in.mobilerechargedth.model.OperatorModel;


import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class OperatorScreenActivity extends BaseActivity {

    ImageButton btnGo;
    Button backbtn;
    Spinner spnOperator;
    long operatorId = 0;
    private TextView _disclaimer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_operator_screen);

        btnGo = (ImageButton) findViewById(R.id.btnGo);
        backbtn = (Button) findViewById(R.id.backbtn);
        backbtn.setVisibility(View.VISIBLE);
        backbtn.setOnClickListener(this);
        spnOperator = (Spinner) findViewById(R.id.spnOperator);

        btnGo.setOnClickListener(this);
        new OperatorTask().execute(new String[] {});
        _disclaimer = (TextView) findViewById(R.id.txtDisclaimer);
        _disclaimer.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent i = new Intent(OperatorScreenActivity.this,
                        DisclaimerActivity.class);
                startActivity(i);

            }
        });
        setupAdmob();
        share();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        super.onClick(v);

        switch (v.getId()) {
        case R.id.btnGo:
            Intent i = new Intent(OperatorScreenActivity.this,
                    SecondScreenActivity.class);
            i.putExtra("OPERATOR_ID", operatorId);
            startActivity(i);
            break;
        case R.id.backbtn:
            this.onBackPressed();
            break;
        }
    }


    /**
     * Async Task to proceed Operator.
     */
    private class OperatorTask extends AsyncTask<String, Void, List<OperatorModel>> {

        private ProgressDialog mProgress;


        @Override
        protected void onPreExecute() {
            mProgress = new ProgressDialog(OperatorScreenActivity.this);
            mProgress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            mProgress.setMessage("Loading...");
            mProgress.setCanceledOnTouchOutside(false);
            // Check press back key when showing dialog.
            mProgress.setOnKeyListener(new DialogInterface.OnKeyListener() {

                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    // TODO Auto-generated method stub
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        return true;
                    }
                    return false;
                }
            });
            mProgress.show();

            super.onPreExecute();
        }

        @Override
        protected List<OperatorModel> doInBackground(String... params) {
            OperatorApi api = new OperatorApi();
            List<OperatorModel> data = null;
            try {
                data = api.getOperator();
            } catch (Exception e) {
            }

            return data;
        }

        @Override
        protected void onPostExecute(List<OperatorModel> result) {
            super.onPostExecute(result);

            if (result != null) {
                final SpinnerOperatorAdapter adapter = new SpinnerOperatorAdapter(OperatorScreenActivity.this, result);
                spnOperator.setAdapter(adapter);
                spnOperator.setOnItemSelectedListener(new OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> arg0, View arg1,
                            int arg2, long arg3) {
                        // TODO Auto-generated method stub
                        operatorId = adapter.getItemId(arg2);
                        OperatorModel final_result = (OperatorModel) adapter.getItem(arg2);
                        CustomSharedPreferences.setPreferences(ConstantVariables.CUSTOM_SHAREDPREFERNCES_OPERATOR_NAME, final_result.geOperatorName());
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> arg0) {
                        // TODO Auto-generated method stub

                    }
                });
            }
            if (mProgress != null) {
                mProgress.dismiss();
                mProgress = null;
            }

        }

    }
}